﻿using UnityEngine;
using System.Collections;
using UniLua;

namespace TCG.FFI
{
	/// <summary>
	/// gettexture/setvector/getvector未实现.
	/// </summary>
	public static class UnityMaterialLib  
	{
		public static string LIB_NAME = "UnityMaterialLib";

		public static string ParentLibName = "UnityObjectLib";

		public static int mf_GetMainColor (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			LuaTools.L_PushUserData (luaState, mat.color, UnityColorLib.LIB_NAME);
			return 1;
		}

		public static int mf_SetMainColor (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			mat.color = (Color)LuaTools.L_CheckUData (luaState, 2, UnityColorLib.LIB_NAME);
			return 0;
		}

		public static int mf_SetMainTexture (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			Texture texture = luaState.ToObject (2) as Texture;
			mat.mainTexture = texture;                              
			return 0;
		}

		public static int mf_GetMainTextureOffset (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			Vector2 offset = mat.mainTextureOffset;
			LuaTools.L_PushUserData (luaState, new Vector3 (offset.x, offset.y), UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_SetMainTextureOffset (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			Vector3 offset = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			mat.mainTextureOffset = new Vector2 (offset.x, offset.y);
			return 0;
		}

		public static int mf_GetMainTextureScale (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			Vector2 scale = mat.mainTextureScale;
			LuaTools.L_PushUserData (luaState, new Vector3 (scale.x, scale.y), UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_SetMainTextureScale (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			Vector3 scale = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			mat.mainTextureScale = new Vector2 (scale.x, scale.y);
			return 0;
		}

		public static int mf_GetPassCount (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			luaState.PushInteger (mat.passCount);
			return 1;
		}

		public static int mf_GetRenderQueue (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			luaState.PushInteger (mat.renderQueue);
			return 1;
		}

		public static int mf_GetShader (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			LuaTools.L_PushUserData (luaState, mat.shader, UnityShaderLib.LIB_NAME);
			return 1;
		}

		public static int mf_SetShader (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			mat.shader = LuaTools.L_CheckUData (luaState, 2, UnityShaderLib.LIB_NAME) as Shader;
			return 0;
		}

		public static int mf_GetColor (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			string propertyName = luaState.ToString (2);
			LuaTools.L_PushUserData (luaState, mat.GetColor (propertyName), UnityColorLib.LIB_NAME);
			return 1;
		}

		public static int mf_GetFloat (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			string propertyName = luaState.ToString (2);
			luaState.PushNumber (mat.GetFloat (propertyName));
			return 1;
		}

		public static int mf_GetInt (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			string propertyName = luaState.ToString (2);
			luaState.PushInteger (mat.GetInt (propertyName));
			return 1;
		}

		public static int mf_GetTextureOffset (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			string propertyName = luaState.ToString (2);
			Vector2 offset = mat.GetTextureOffset (propertyName);
			LuaTools.L_PushUserData (luaState, new Vector3 (offset.x, offset.y), UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_GetTextureScale (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			string propertyName = luaState.ToString (2);
			Vector2 scale = mat.GetTextureScale (propertyName);
			LuaTools.L_PushUserData (luaState, new Vector3 (scale.x, scale.y), UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_HasProperty (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			string propertyName = luaState.ToString (2);
			luaState.PushBoolean (mat.HasProperty (propertyName));
			return 1;
		}

		public static int mf_SetColor (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			string propertyName = luaState.ToString (2);
			Color color = (Color)LuaTools.L_CheckUData (luaState, 3, UnityColorLib.LIB_NAME);
			mat.SetColor (propertyName, color);
			return 0;
		}

		public static int mf_SetFloat (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			string propertyName = luaState.ToString (2);
			float value = (float)luaState.ToNumber (3);
			mat.SetFloat (propertyName, value);
			return 0;
		}

		public static int mf_SetInt (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			string propertyName = luaState.ToString (2);
			int value = luaState.ToInteger (3);
			mat.SetInt (propertyName, value);
			return 0;
		}

		public static int mf_SetPass (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			int pass = luaState.ToInteger (2);
			mat.SetPass (pass);
			return 0;
		}

		public static int mf_SetTextureOffset (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			string propertyName = luaState.ToString (2);
			Vector3 offset = (Vector3)LuaTools.L_CheckUData (luaState, 3, UnityVector3Lib.LIB_NAME);
			mat.SetTextureOffset (propertyName, new Vector2 (offset.x, offset.y));
			return 0;
		}

		public static int mf_SetTextureScale (ILuaState luaState)
		{
			Material mat = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Material;
			string propertyName = luaState.ToString (2);
			Vector3 scale = (Vector3)LuaTools.L_CheckUData (luaState, 3, UnityVector3Lib.LIB_NAME);
			mat.SetTextureScale (propertyName, new Vector2 (scale.x, scale.y));
			return 0;
		}
	}
}